/**
 * 楼宇管理
 */
import { Op } from 'sequelize'
import uuid from 'uuid'
import BaseBusinessController from './../base/BaseBusinessController'

class AdminBuildingController extends BaseBusinessController {

    /**
     * 楼宇列表
     * @return {[type]} [description]
     */
    async actionColumn() {
        let where = {};

        let buildingIds = await super.buildingIds(this.state.user.id);
        if (buildingIds) {
            where['id'] = {
                [Op.in]:buildingIds
            }
        }

        if ($_POST['search']) {
            where['name'] = {
                [Op.like]: '%' + $_POST['search'] + '%'
            }
        }

        return await this.modules('Building').where(where).fields(['id', 'name','region_id']).order([
            ['id', 'DESC']
        ]).select();
    }

    /**
     * 楼宇列表
     * @return {[type]} [description]
     */
    async actionIndex() {
        let page = $_POST['page'];
        let number = page && page.number !== undefined ? page.number : 1;
        let size = page && page.size !== undefined ? page.size : 20;
        let where = {};

        let buildingIds = await super.buildingIds(this.state.user.id);
        if (buildingIds) {
            where['id'] = {
                [Op.in]:buildingIds
            }
        }

        if ($_POST['search']) {
            where['name'] = {
                [Op.like]: '%' + $_POST['search'] + '%'
            }
        }
        if ($_POST['region_id']) {
            where['region_id'] = $_POST['region_id'];
        }

        return await this.modules('Building').where(where).joinWith(['region']).page(number, size).order([
            ['id', 'DESC']
        ]).select();
    }

    /**
     * 楼宇详情
     * @return {[type]} [description]
     */
    async actionView() {
        let id = $_POST['id'];
        let data = await this.modules('Building').where({ id }).joinWith(['region']).first();
        if (data) {
            return data;
        } else {
            throw new Error('数据不存在');
        }
    }

    /**
     * 新增楼宇
     * @return {[type]} [description]
     */
    async actionCreate() {
        let data = await this.modules('Building').insert($_POST);
        if (data) {
            return data;
        } else {
            throw new Error('保存失败');
        }
    }
    /**
     * 新增楼宇(批量)
     * @returns 
     */
    async actionBulkCreate() {
        let data = await this.modules('Building').bulkCreate($_POST.upData);
        if (data) {
            return data;
        } else {
            throw new Error('保存失败');
        }
    }
    /**
     * 编辑楼宇
     * @return {[type]} [description]
     */
    async actionUpdate() {
        let id = $_POST['id'];
        let data = await this.modules('Building').where({ id }).update($_POST);
        if (data) {
            return data;
        } else {
            throw new Error('保存失败');
        }
    }

    /**
     * 删除楼宇
     * @return {[type]} [description]
     */
    async actionDelete() {
        let id = $_POST['id'];
        let res = await this.modules('Building').where({ id }).delete();
        if (res) {
            return true;
        } else {
            throw new Error('删除失败');
        }
    }

    /**
     * 楼宇闸道门列表
     * @return {[type]} [description]
     */
    async actionLockColumn() {
        let where = {};
        if ($_POST['search']) {
            where['code'] = {
                [Op.like]: '%' + $_POST['search'] + '%'
            }
        }

        if ($_POST['building_id']) {
            where['building_id'] = $_POST['building_id'];
        }

        return await this.modules('Building.LockDoor').where(where).fields(['id', 'code','note']).order([
            ['id', 'DESC']
        ]).select();
    }

    /**
     * 楼宇闸道门列表
     * @return {[type]} [description]
     */
    async actionLockIndex() {
        let page = $_POST['page'];
        let number = page && page.number !== undefined ? page.number : 1;
        let size = page && page.size !== undefined ? page.size : 20;
        let where = {};
        if ($_POST['search']) {
            where['code'] = {
                [Op.like]: '%' + $_POST['search'] + '%'
            }
        }

        if ($_POST['building_id']) {
            where['building_id'] = $_POST['building_id'];
        }

        return await this.modules('Building.LockDoor').where(where).page(number, size).order([
            ['id', 'DESC']
        ]).select();
    }

    /**
     * 楼宇闸道门详情
     * @return {[type]} [description]
     */
    async actionLockView() {
        let id = $_POST['id'];
        let data = await this.modules('Building.LockDoor').where({ id }).first();
        if (data) {
            return data;
        } else {
            throw new Error('数据不存在');
        }
    }

    /**
     * 新增楼宇闸道门
     * @return {[type]} [description]
     */
    async actionLockCreate() {
        let data = await this.modules('Building.LockDoor').insert($_POST);
        if (data) {
            return data;
        } else {
            throw new Error('保存失败');
        }
    }

    /**
     * 编辑楼宇闸道门
     * @return {[type]} [description]
     */
    async actionLockUpdate() {
        let id = $_POST['id'];
        let data = await this.modules('Building.LockDoor').where({ id }).update($_POST);
        if (data) {
            return data;
        } else {
            throw new Error('保存失败');
        }
    }

    /**
     * 删除楼宇闸道门
     * @return {[type]} [description]
     */
    async actionLockDelete() {
        let id = $_POST['id'];
        let res = await this.modules('Building.LockDoor').where({ id }).delete();
        if (res) {
            return true;
        } else {
            throw new Error('删除失败');
        }
    }

}

export default AdminBuildingController;